<?php
// 应用公共文件
use think\Db;

function getCategory($value, $filed = 'category_name')
{
    if ($value == 0)
    {
        return '顶级栏目';
    }
   $res = Db::name('category')->where('category_id', $value)->find();
   return is_null($res) ? '未知' : $res[$filed];
}

function make_tree($arr)
{
    if (!function_exists('make_tree1')) {
        function make_tree1($arr, $pid = 0)
        {
            $new_arr = array();
            foreach ($arr as $k => $v) {
                if ($v->pid == $pid) {
                    $new_arr[] = $v;
                    unset($arr[$k]);
                }
            }
            foreach ($new_arr as &$a) {
                $a->children = make_tree1($arr, $a->category_id);
            }
            return $new_arr;
        }
    }
    return make_tree1($arr);
}

function make_tree_with_namepre($arr)
{
    $arr = make_tree($arr);
    if (!function_exists('add_namepre1')) {
        function add_namepre1($arr, $prestr = '')
        {
            $new_arr = array();
            foreach ($arr as $v) {
                if ($prestr) {
                    if ($v == end($arr)) {
                        $v->category_name = $prestr . '└─ ' . $v->category_name;
                    } else {
                        $v->category_name = $prestr . '├─ ' . $v->category_name;
                    }
                }

                if ($prestr == '') {
                    $prestr_for_children = '　 ';
                } else {
                    if ($v == end($arr)) {
                        $prestr_for_children = $prestr . '　　 ';
                    } else {
                        $prestr_for_children = $prestr . '│　 ';
                    }
                }
                $v->children = add_namepre1($v->children, $prestr_for_children);

                $new_arr[] = $v;
            }
            return $new_arr;
        }
    }
    return add_namepre1($arr);
}

/**
 * @param $arr
 * @param int $depth 当 $depth为0的时候表示不限制深度
 * @param int $selVal 默认选中的option值
 * @return string
 */
function make_option_tree_for_select($arr, $depth = 0, $selVal = 0)
{
    $arr = make_tree_with_namepre($arr);
    if (!function_exists('make_options1')) {
        function make_options1($arr, $depth, $recursion_count = 0, $ancestor_ids = '', $selVal = 0)
        {
            $recursion_count++;
            $str = "";

            foreach ($arr as $v) {
                if ($v->category_id == $selVal)
                {
                    $str .= "<option value='{$v->category_id}' data-depth='{$recursion_count}' data-ancestor_ids='" . ltrim($ancestor_ids, ',') . "' selected>{$v->category_name}</option>";
                }
                else
                {
                    $str .= "<option value='{$v->category_id}' data-depth='{$recursion_count}' data-ancestor_ids='" . ltrim($ancestor_ids, ',') . "'>{$v->category_name}</option>";
                }
                if ($v->pid == 0) {
                    $recursion_count = 1;
                }
                if ($depth == 0 || $recursion_count < $depth) {
                    $str .= make_options1($v->children, $depth, $recursion_count, $ancestor_ids . ',' . $v->category_id, $selVal);
                }

            }
            return $str;
        }
    }
    return make_options1($arr, $depth, 0 , '', $selVal);
}

/**
 * @param $table 传入表名
 * @param string $id 是否需要带id查询
 */
function read($table, $id = '')
{
    if (empty($id))
    {
        $data = Db::name($table)->where($id)->find();
    }else{
        $data = Db::name($table)->select();
    }
    return $data;
}